package com.besttone.rabbit.consumer;

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitMQConfig {


	/**
	 * 注入配置文件属性
	 */
	@Value("${spring.rabbitmq.addresses}")
	String addresses;// MQ地址
	@Value("${spring.rabbitmq.username}")
	String username;// MQ登录名
	@Value("${spring.rabbitmq.password}")
	String password;// MQ登录密码
	@Value("${spring.rabbitmq.virtual-host}")
	String vHost;// MQ的虚拟主机名

	/**
	 * 创建 ConnectionFactory
	 *
	 * @return
	 * @throws Exception
	 */
	@Bean
	public ConnectionFactory connectionFactory() throws Exception {
		return connectionFactory(addresses, username, password, vHost);
	}

	public static ConnectionFactory connectionFactory(String addresses, String username, String password, String vHost)
			throws Exception {
		CachingConnectionFactory factoryBean = new CachingConnectionFactory();
		factoryBean.setVirtualHost(vHost);
		factoryBean.setAddresses(addresses);
		factoryBean.setUsername(username);
		factoryBean.setPassword(password);
		return factoryBean;
	}

	/**
	 * 创建 RabbitAdmin
	 *
	 * @param connectionFactory
	 * @return
	 * @throws Exception
	 */
	@Bean
	public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) throws Exception {
		RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
		return rabbitAdmin;
	}

}
